package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.CollectVirCommandInst;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 12:16
 * @description
 */
@Repository("collectVirCommandInstArchiverDao")
public class CollectVirCommandInstArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM COLLECT_VIR_COMMAND_INST WHERE TASK_ID = ?");
        List<CollectVirCommandInst> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(CollectVirCommandInst.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `INST_ID`, `TASK_ID`, `V_CMD_CODE`, `CREATE_TIME`, `FINISH_TIME`, `STATUS`, `SOURCE_CODE`, `PARTITION_CODE`, `REDIS_CLUSTER_CODE`, `DIRECTION`, `EXE_SEQ`, `API_ID`");
        sql.append(" FROM COLLECT_VIR_COMMAND_INST WHERE TASK_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<CollectVirCommandInst> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                CollectVirCommandInst collectVirCommandInst = new CollectVirCommandInst();
                collectVirCommandInst.setInstId(resultSet.getLong("INST_ID"));
                collectVirCommandInst.setTaskId(resultSet.getLong("TASK_ID"));
                collectVirCommandInst.setvCmdCode(resultSet.getString("V_CMD_CODE"));
                collectVirCommandInst.setStatus(resultSet.getString("STATUS"));
                collectVirCommandInst.setCreateTime(resultSet.getString("CREATE_TIME"));
                collectVirCommandInst.setFinishTime(resultSet.getString("FINISH_TIME"));
                collectVirCommandInst.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                collectVirCommandInst.setRedisClusterCode(resultSet.getString("REDIS_CLUSTER_CODE"));
                collectVirCommandInst.setDirection(resultSet.getInt("DIRECTION"));
                collectVirCommandInst.setExeSeq(resultSet.getInt("EXE_SEQ"));
                collectVirCommandInst.setApiId(resultSet.getString("API_ID"));
                collectVirCommandInst.setSourceCode(resultSet.getString("SOURCE_CODE"));
                return collectVirCommandInst;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `collect_vir_command_inst`");
        sql.append(" (`INST_ID`, `TASK_ID`, `V_CMD_CODE`, `CREATE_TIME`, `FINISH_TIME`, `STATUS`, `SOURCE_CODE`, `PARTITION_CODE`, `REDIS_CLUSTER_CODE`, `DIRECTION`, `EXE_SEQ`, `API_ID`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:instId,:taskId,:vCmdCode,:createTime,:finishTime,:status,:sourceCode,:partitionCode,:redisClusterCode,:direction,:exeSeq,:apiId,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {

    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM COLLECT_COMMAND_INST WHERE INST_ID = :instId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
